'''
Created on Aug 13, 2009

@author: Stanly
'''
import pygame, os, constants

def load_image(fullname, colorkey=None):
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', fullname
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, pygame.RLEACCEL)
    return image, image.get_rect()

class MapObject(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y

class Ship(MapObject):
    def __init__(self, name, type, faction, x, y):
        MapObject.__init__(self, x, y)
        self.name = name
        self.type = type
        self.faction = faction

class Star(MapObject):
    def __init__(self, system, name, size, type, x, y):
        MapObject.__init__(self, x, y)
        self.image, self.rect = load_image(os.path.join(system.map.resource_path, 'stars/small_star_1.png'))
        self.rect.left = x
        self.rect.top = y
        self.info_image, self.info_rect = load_image(os.path.join(system.map.resource_path, 'stars/big_star_1.png'))
        self.name = name
        self.type = type
        self.size = int(size)
    
class Planet(MapObject):
    def __init__(self, star, name, type, color, size, x, y):
        MapObject.__init__(self, x, y)
        self.name = name
        self.type = type
        self.size = size
        self.star = star

class System:
    def __init__(self, map, name, dom):
        self.map = map
        self.name = name
        self.dom = dom
        self.width = int(dom.getAttribute('width'))
        self.height = int(dom.getAttribute('height'))
        self.planets = {}
        self.objects = {}
        self.visible_sprites = pygame.sprite.LayeredUpdates()
        star = self.dom.getElementsByTagName('star')[0]
        #create a star
        self.star = Star(self, star.getAttribute('name'),
                         int(star.getAttribute('size')),
                         star.getAttribute('type'),
                         int(star.getAttribute('x')),
                         int(star.getAttribute('y')))
        #create planets
        for n in self.dom.getElementsByTagName('planet'):
            planet_name = n.attributes['name'].nodeValue
            self.planets[planet_name] = Planet(self.star,
                                               planet_name,
                                               n.getAttribute('type'),
                                               n.getAttribute('color'),
                                               int(n.getAttribute('size')),
                                               int(n.getAttribute('x')),
                                               int(n.getAttribute('y')))

    def update(self, cell_visible_x, cell_visible_y, viewport_w, viewport_h):
        #check if start in visible
        if self.star.x >= cell_visible_x \
            and self.star.y >= cell_visible_y \
            and self.star.x <= cell_visible_x + viewport_w - 1\
            and self.star.y <= cell_visible_y + viewport_h - 1:
            self.draw_sprite(self.star, cell_visible_x, cell_visible_y)
        else:
            #start is invisible
            if len(self.star.groups()) > 0:
                self.star.kill()
        
#        for planet_name in self.planets:
#            if self.planets[planet_name].x >= cell_visible_x \
#            and self.planets[planet_name].y >= cell_visible_y \
#            and self.planets[planet_name].x <= cell_visible_x + viewport_w - 1 \
#            and self.planets[planet_name].y <= cell_visible_y + viewport_h - 1:
#                self.draw_sprite(self.star, cell_visible_x, cell_visible_y)
#                self.planets[planet_name].update(cell_visible_x, cell_visible_y)
#                
#        for obj_name in self.objects:
#            if self.objects[obj_name].x >= cell_visible_x \
#            and self.objects[obj_name].y >= cell_visible_y \
#            and self.objects[obj_name].x <= cell_visible_x + viewport_w - 1 \
#            and self.objects[obj_name].y <= cell_visible_y + viewport_h - 1:
#                self.objects[obj_name].update(cell_visible_x, cell_visible_y)
                
    def draw_sprite(self, sprite, visible_x, visible_y):
        x = (sprite.x - visible_x) * constants.cell_width
        y = (sprite.y - visible_y) * constants.cell_height
        if hasattr(sprite, 'rect'):
            sprite.rect.left = x + constants.viewport_margin_w * 2
            sprite.rect.top = y + constants.viewport_margin_h * 2
            #make sprite visible
            if not self.visible_sprites in sprite.groups():
                self.visible_sprites.add(sprite)
        else:
            name = 'unnamed'
            if hasattr(self, 'name'):
                name = self.name
            print 'DRAW PASS \'%s\' at %s:%s' % (name, x, y)
